package com.ald.crossex.service.wechat;

import com.ald.crossex.dao.CeCoreCityDao;
import com.ald.crossex.dao.CeCoreProvinceDao;
import com.ald.crossex.dao.CeCoreZoneDao;
import com.ald.crossex.dao.CeJourneyDao;
import com.ald.crossex.dto.wechat.*;
import com.ald.crossex.entity.TCeCoreCity;
import com.ald.crossex.entity.TCeCoreProvince;
import com.ald.crossex.entity.TCeCoreZone;
import com.ald.crossex.entity.TCeJourney;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by lishijiong on 15/3/11.
 */
@Service
public class JourneyService {
    @Autowired
    private CeCoreProvinceDao provinceDao;
    @Autowired
    private CeCoreCityDao cityDao;
    @Autowired
    private CeCoreZoneDao zoneDao;
    @Autowired
    private CeJourneyDao journeyDao;


    /**
     * 获取已经开通的城市
     * @return
     */
    public OpenPlaceResponse openPlace(){

        OpenPlaceResponse dto = new OpenPlaceResponse();
        List<OpenProvince> openProvinces = new ArrayList<OpenProvince>();
        List<TCeCoreProvince> provinces = provinceDao.queryByHql("from TCeCoreProvince where active = 1 order by provinceSort",new Object[]{});
        for(TCeCoreProvince p :provinces){
            OpenProvince op = new OpenProvince();
            BeanUtils.copyProperties(p,op);

            List<OpenCity> openCities = new ArrayList<OpenCity>();
            List<TCeCoreCity> cities = cityDao.queryByHql("from TCeCoreCity where active = 1 and provinceId = ? order by citySort",new Object[]{op.getProvinceId()});
            for(TCeCoreCity c:cities){
                OpenCity oc = new OpenCity();
                BeanUtils.copyProperties(c,oc);

                List<OpenZone> openZones = new ArrayList<OpenZone>();
                List<TCeCoreZone> zones = zoneDao.queryByHql("from TCeCoreZone where active = 1 and cityId = ? order by zoneSort", new Object[]{oc.getCityId()});

                for(TCeCoreZone z:zones){
                    OpenZone oz = new OpenZone();
                    BeanUtils.copyProperties(z,oz);
                    openZones.add(oz);
                }
                oc.setZones(openZones);
                openCities.add(oc);
            }
            op.setCitys(openCities);
            openProvinces.add(op);
        }

        dto.setProvinces(openProvinces);

        return dto;

    }

    /**
     * 发布行程
     * @param request
     */
    @Transactional
    public BaseResponse publishJourney(PublishJourneyRequest request){

        TCeJourney journey = new TCeJourney();
        journey.setUid(request.getUid());
        journey.setStartTime(request.getStartTime());
        journey.setEndTime(request.getEndTime());
        journey.setType(request.getType());
        journey.setStartProvinceId(request.getStartProvinceId());
        journey.setStartCityId(request.getStartCityId());
        journey.setStartZoneId(request.getStartZoneId());
        journey.setEndProvinceId(request.getEndProvinceId());
        journey.setEndCityId(request.getEndCityId());
        journey.setEndZoneId(request.getEndZoneId());
        journey.setPhone(request.getPhone());
        journey.setRemark(request.getRemark());
        journey.setFee(request.getFee());
        journey.setTrafficType(request.getTrafficType());
        journey.setCreateTime(new Date());
        journey.setUpdateTime(new Date());
        journey.setIsDelete(0);
        journeyDao.create(journey);

        BaseResponse response = new BaseResponse();
        response.setResultMsg("您的行程发布成功！");
        return response;

    }


}
